package br.com.lazyprogrammer.social.infestit;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import br.com.lazyprogrammer.social.infestit.adapter.MenuAdapter;
import br.com.lazyprogrammer.social.infestit.fragment.ProfileFragment;
import br.com.lazyprogrammer.social.infestit.fragment.menu.BlessFragment;
import br.com.lazyprogrammer.social.infestit.fragment.menu.CurseFragment;
import br.com.lazyprogrammer.social.infestit.fragment.menu.FeedBlessFragment;
import br.com.lazyprogrammer.social.infestit.fragment.menu.FeedCurseFragment;
import br.com.lazyprogrammer.social.infestit.util.PoolData;

/**
 * Created by Andre on 10/09/2014.
 */
public class HomeActivity extends Activity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private MenuAdapter menuAdapter;

    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    private ImageView mAvatar;
    private ListView mDrawerList;
    private LinearLayout mDrawerMenu;
    private RelativeLayout mUserInfo;
    private TextView mDescription;
    private TextView mUsername;

    @Override
    public void onClick(View view) {
        if (view.equals(mUserInfo))
            selectItem(99);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        setContentView(R.layout.activity_home);

        PoolData.start();

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_menu,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_closed  /* "close drawer" description */
        ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                //getActionBar().setTitle(mTitle);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                //getActionBar().setTitle(mDrawerTitle);
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        menuAdapter = new MenuAdapter(this);
        mDrawerList.setAdapter(menuAdapter);
        mDrawerList.setOnItemClickListener(this);

        mDrawerMenu = (LinearLayout) findViewById(R.id.drawer_menu);
        mAvatar = (ImageView) findViewById(R.id.image_avatar);
        mUserInfo = (RelativeLayout) findViewById(R.id.header_user_info);
        mDescription = (TextView) findViewById(R.id.label_description);
        mUsername = (TextView) findViewById(R.id.label_username);

        mUserInfo.setClickable(true);
        mUserInfo.setOnClickListener(this);

        if (PoolData.user != null) {
            PoolData.downloader.download(getString(R.string.avatar_url, PoolData.user.getId()), mAvatar);
            mDescription.setText(PoolData.user.getDescription());
            mUsername.setText(PoolData.user.getUsername());
        }
        else {
            PoolData.downloader.download(getString(R.string.common_avatar_url), mAvatar);
            mUsername.setText(getString(R.string.label_visitor));
            // enviar aviso q apenas usuários cadastrados podem realizar
            // requisições
        }

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        selectItem(0);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    /** Swaps fragments in the main content view */
    private void selectItem(int position) {
        // Create a new fragment and specify the planet to show based on position
        Fragment fragment = null;
        if (position == 0)
            fragment = new FeedCurseFragment();
        else if (position == 1)
            fragment = new FeedBlessFragment();
        else if (position == 2)
            fragment = new CurseFragment();
        else if (position == 3)
            fragment = new BlessFragment();
        else if (position == 99)
            fragment = new ProfileFragment();

        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment)
                .commit();

        // Highlight the selected item, update the title, and close the drawer
        mDrawerLayout.closeDrawer(mDrawerMenu);
        mDrawerList.setItemChecked(position, true);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        selectItem(position);
    }
}
